//
//  SchoolInfoViewController.m
//  JYT_IOS
//
//  Created by nxa-tankeke on 17/1/6.
//  Copyright © 2017年 nxa-yuanguoxiang. All rights reserved.
//

#import "SchoolInfoViewController.h"
#import "JYTSchoolHeader.h"
#import "JYTJianJieCell.h"
#import "CourseViewController.h"
#import "JYTBaoMingCell.h"
#import "JYTXinxiCell.h"
#import "JYTHuodongCell.h"
#import "JYTHuodongViewController.h"
#import "JYTJiaxiaojlViewController.h"
#import "JYTLianView.h"
#import "JYTCoachHandler.h"
#import "School.h"

@interface SchoolInfoViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *commentArray;
@property (nonatomic, assign) CGFloat cellHeight;

@property (nonatomic, strong)NSMutableArray * coaches;
@property (nonatomic, strong) UIWebView *web;

@end

@implementation SchoolInfoViewController

-(NSMutableArray *)coaches
{
    if (_coaches == nil) {
        _coaches = [NSMutableArray array];
    }
    
    return _coaches;
}

-(NSMutableArray *)commentArray
{
    if (_commentArray == nil) {
        _commentArray = [NSMutableArray array];
    }
    
    return _commentArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSDictionary *dic = @{@"lftBtnBgImg":@"ic_goback",
                          @"lftBtnTitle":@"no",
                          @"pageTitle":@"驾校详情",
                          @"rhtBtnBgImg":@"no",
                          @"rhtBtnTitle":@"no"};
    
    [self setNavgationBarWithDic:dic];
    
    
    NSString *content = @"你就从你家那几年方开具能促进男女纠纷女会计餐具费女君君叫你家妇女福利吃饭不吃覅你就从你家那几年方开具能促进男女纠纷女会计餐具费女君君叫你家妇女福利吃饭不吃覅";
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    CGSize maxS = CGSizeMake([UIScreen mainScreen].bounds.size.width - 25, MAXFLOAT);
    CGFloat contentH = [content boundingRectWithSize:maxS options:NSStringDrawingUsesLineFragmentOrigin attributes:
                        attributes context:nil].size.height;
    _cellHeight = 30 + contentH;
    
    JYTSchoolHeader *header = [JYTSchoolHeader header];
    header.frame = CGRectMake(0, 64, CURRENT_SCREEN_WIDTH, 170);
    header.school = self.school;
  
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CURRENT_SCREEN_WIDTH, CURRENT_SCREEN_HEIGHT - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([JYTJianJieCell class]) bundle:nil] forCellReuseIdentifier:@"jianjie"];
    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([JYTBaoMingCell class]) bundle:nil] forCellReuseIdentifier:@"baoming"];
    [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([JYTXinxiCell class]) bundle:nil] forCellReuseIdentifier:@"xinxi"];
    
     [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([JYTHuodongCell class]) bundle:nil] forCellReuseIdentifier:@"huodong"];
    
    //_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.tableHeaderView = header;
    
    NSMutableDictionary *parms = [NSMutableDictionary dictionary];
    
    [parms setObject:_school.drivingSchoolId forKey:@"DrivingSchoolId"];
    [parms setObject:@"0" forKey:@"PageNo"];
    [parms setObject:@"10" forKey:@"PageSize"];

    [[JYTCoachHandler shared] getSchoolCoachWithParam:parms WithHandler:^(NSArray *coaches, NSString *pageNo) {
        [self.coaches addObjectsFromArray:coaches];
        
        NSIndexPath *indexPath=[NSIndexPath indexPathForRow:9 inSection:0];
        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];

    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return (12 + self.commentArray.count);
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:@"cell"];

        UILabel *adressL = [[UILabel alloc]init];
        adressL.font = [UIFont systemFontOfSize:16];
        [adressL setText:@"驾校地址"];
        [cell.contentView addSubview:adressL];
        [adressL mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(cell.contentView.mas_left).with.offset(15);
            make.centerY.equalTo(cell.contentView.mas_centerY);

        }];
        
        UILabel *adressLL = [[UILabel alloc]init];
        adressLL.font = [UIFont systemFontOfSize:14];
        [adressLL setTextColor:[UIColor lightGrayColor]];
        [adressLL setText:self.school.registerAddress];
        [cell.contentView addSubview:adressLL];
        [adressLL mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(adressL.mas_right).with.offset(10);
            make.centerY.equalTo(cell.contentView.mas_centerY);
            
        }];
        
        return cell;
    } else if(indexPath.row == 1){
        UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:@"cell2"];
        
        UILabel *adressL = [[UILabel alloc]init];
        adressL.font = [UIFont systemFontOfSize:16];
        [adressL setText:@"联系电话"];
        [cell.contentView addSubview:adressL];
        [adressL mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(cell.contentView.mas_left).with.offset(15);
            make.centerY.equalTo(cell.contentView.mas_centerY);
            
        }];
        
        UILabel *adressLL = [[UILabel alloc]init];
        adressLL.font = [UIFont systemFontOfSize:14];
        [adressLL setTextColor:[UIColor lightGrayColor]];
        [adressLL setText:self.school.telephone];
        [cell.contentView addSubview:adressLL];
        [adressLL mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(adressL.mas_right).with.offset(10);
            make.centerY.equalTo(cell.contentView.mas_centerY);
            
        }];
        
        return cell;

    } else if(indexPath.row == 2){
        NSString *content = @"你就从你家那几年方开具能促进男女纠纷女会计餐具费女君君叫你家妇女福利吃饭不吃覅你就从你家那几年方开具能促进男女纠纷女会计餐具费女君君叫你家妇女福利吃饭不吃覅";
        JYTJianJieCell *cell = [tableView dequeueReusableCellWithIdentifier:@"jianjie"];
        cell.jianjie = content;
        return cell;

    } else if(indexPath.row == 3){
        UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:@"cell3"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UILabel *adressL = [[UILabel alloc]init];
        adressL.font = [UIFont systemFontOfSize:16];
        [adressL setText:@"课程培训"];
        [cell.contentView addSubview:adressL];
        [adressL mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(cell.contentView.mas_left).with.offset(15);
            make.centerY.equalTo(cell.contentView.mas_centerY);
            
        }];
        
        UIButton *courBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [courBtn setTitle:@"查看全部" forState:UIControlStateNormal];
        [courBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [courBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
        [courBtn addTarget:self action:@selector(courAllClick) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:courBtn];
        [courBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.equalTo(cell.contentView.mas_right).with.offset(-20);
            make.centerY.equalTo(cell.contentView.mas_centerY);
            
        }];
        return cell;
    } else if(indexPath.row == 4){
        
        JYTBaoMingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"baoming"];
        return cell;

    } else if(indexPath.row == 5){
        JYTXinxiCell *cell = [tableView dequeueReusableCellWithIdentifier:@"xinxi"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.school = self.school;
        return cell;
    } else if(indexPath.row == 6){
        UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:@"cell6"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UILabel *adressL = [[UILabel alloc]init];
        adressL.font = [UIFont systemFontOfSize:16];
        [adressL setText:@"驾校全称"];
        [cell.contentView addSubview:adressL];
        [adressL mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(cell.contentView.mas_left).with.offset(15);
            make.centerY.equalTo(cell.contentView.mas_centerY);
            
        }];
        
        UILabel *adressLL = [[UILabel alloc]init];
        adressLL.font = [UIFont systemFontOfSize:14];
        [adressLL setTextColor:[UIColor lightGrayColor]];
        [adressLL setText:self.school.drivingSchoolName];
        [cell.contentView addSubview:adressLL];
        [adressLL mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(adressL.mas_right).with.offset(10);
            make.centerY.equalTo(cell.contentView.mas_centerY);
            
        }];
        
        return cell;
    } else if(indexPath.row == 7){

        JYTHuodongCell *cell = [tableView dequeueReusableCellWithIdentifier:@"huodong"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.school = self.school;
        [cell setResultBlock:^() {
            JYTHuodongViewController *huodong = [[JYTHuodongViewController alloc]init];
            [self pushViewController:huodong];
            
        }];
        return cell;
    } else if(indexPath.row == 8){

        UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:@"cell8"];
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UILabel *adressL = [[UILabel alloc]init];
        adressL.font = [UIFont systemFontOfSize:16];
        [adressL setText:@"驾校教练"];
        [cell.contentView addSubview:adressL];
        [adressL mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(cell.contentView.mas_left).with.offset(15);
            make.centerY.equalTo(cell.contentView.mas_centerY);
            
        }];
        
        UIButton *courBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [courBtn setTitle:@"查看全部" forState:UIControlStateNormal];
        [courBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [courBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
        [courBtn addTarget:self action:@selector(jiaxiaoJiaolian) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:courBtn];
        [courBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.equalTo(cell.contentView.mas_right).with.offset(-20);
            make.centerY.equalTo(cell.contentView.mas_centerY);
            
        }];
        
        
        return cell;
    } else if(indexPath.row == 9){
        UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:@"cell9"];
        JYTLianView *jiaoLianOne = [JYTLianView jiaolian];
        if (self.coaches.count >= 1) {
            jiaoLianOne.appointModel = self.coaches[0];
           // jiaoLianOne.sView.hidden = NO;
        } else {
            jiaoLianOne.sView.hidden = YES;
        }
       // JYTAppointModel *one = self.coaches[0];
        //NSLog(@"*****%@", one.grade_value);
        [cell.contentView addSubview:jiaoLianOne];
        [jiaoLianOne mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(cell.contentView.mas_left).with.offset(0);
            CGFloat W = CURRENT_SCREEN_WIDTH / 3;
            CGFloat H = 80;
             make.width.mas_equalTo(W);
             make.height.mas_equalTo(H);
            make.centerY.equalTo(cell.contentView.mas_centerY);
        }];
        
        JYTLianView *jiaoLianTwo = [JYTLianView jiaolian];
        if (self.coaches.count >= 2) {
            jiaoLianTwo.appointModel = self.coaches[1];
            //jiaoLianTwo.sView.hidden = NO;
        } else {
            jiaoLianTwo.sView.hidden = YES;
        }
        [cell.contentView addSubview:jiaoLianTwo];
        [jiaoLianTwo mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(jiaoLianOne.mas_right).with.offset(0);
            CGFloat W = CURRENT_SCREEN_WIDTH / 3;
            CGFloat H = 80;
            make.width.mas_equalTo(W);
            make.height.mas_equalTo(H);
            make.centerY.equalTo(cell.contentView.mas_centerY);
        }];
        
        JYTLianView *jiaoLianThree = [JYTLianView jiaolian];
        if (self.coaches.count >= 3) {
            jiaoLianThree.appointModel = self.coaches[2];
            //jiaoLianTwo.sView.hidden = NO;
        } else {
            jiaoLianThree.sView.hidden = YES;
        }
        [cell.contentView addSubview:jiaoLianThree];
        [jiaoLianThree mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(jiaoLianTwo.mas_right).with.offset(0);
            CGFloat W = CURRENT_SCREEN_WIDTH / 3;
            CGFloat H = 80;
            make.width.mas_equalTo(W);
            make.height.mas_equalTo(H);
            make.centerY.equalTo(cell.contentView.mas_centerY);
        }];
        
        return cell;
    } else if(indexPath.row == 10){
        UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:@"cell10"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        UILabel *adressL = [[UILabel alloc]init];
        adressL.font = [UIFont systemFontOfSize:16];
        [adressL setText:@"学员评论"];
        [cell.contentView addSubview:adressL];
        [adressL mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.equalTo(cell.contentView.mas_left).with.offset(15);
            make.centerY.equalTo(cell.contentView.mas_centerY);
            
        }];
        
        UIButton *courBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [courBtn setTitle:@"查看全部" forState:UIControlStateNormal];
        [courBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        [courBtn.titleLabel setFont:[UIFont systemFontOfSize:13]];
        [courBtn addTarget:self action:@selector(commentClick) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:courBtn];
        [courBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.right.equalTo(cell.contentView.mas_right).with.offset(-20);
            make.centerY.equalTo(cell.contentView.mas_centerY);
            
        }];
        return cell;

        
    } else {
        UITableViewCell * cell = [[UITableViewCell alloc] initWithStyle:0 reuseIdentifier:@"celln"];
        cell.textLabel.text = @"历史搜索";
        cell.textLabel.textColor = [UIColor lightGrayColor];
        return cell;
    }

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 2) {
        return _cellHeight;
    } else if(indexPath.row == 4){
        return 85;
    } else if(indexPath.row == 5){
        return 80;
    }else if(indexPath.row == 7){
        return 83;
    }else if (indexPath.row == 9){
        return 80;
    }else {
        return 44;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.row == 1) {
      
        NSString *phone = [NSString stringWithFormat:@"是否拨打%@",self.school.telephone];
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提 示" message:phone preferredStyle: UIAlertControllerStyleAlert];
        
        //@“ UIAlertControllerStyleAlert，改成这个就是中间弹出"
        
        UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"是" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.web = [[UIWebView alloc] initWithFrame:CGRectZero];
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", self.school.telephone]];
            MyLog(@"tel:%@",self.school.telephone);
            [self.web loadRequest:[NSURLRequest requestWithURL:url]];
            
        }];
       UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"否" style:UIAlertActionStyleCancel handler:nil];
         [alertController addAction:cancelAction];
        [alertController addAction:deleteAction];
       
        
        //            [alertController addAction:archiveAction];
        [self presentViewController:alertController animated:YES completion:nil];
       
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)courAllClick
{
    CourseViewController *course = [[CourseViewController alloc]init];
    [self pushViewController:course];
}


- (void)jiaxiaoJiaolian
{
    JYTJiaxiaojlViewController *jl = [[JYTJiaxiaojlViewController alloc]init];
    jl.school = self.school;
    [self pushViewController:jl];
}

- (void)commentClick
{
   
}
@end
